package cn.swenty.backstage.framework.database.entity;

import java.util.ArrayList;
import java.util.List;

import cn.swenty.backstage.framework.common.FormMap;
import cn.swenty.backstage.framework.database.annotation.TableSeg;
import cn.swenty.backstage.framework.database.mapper.BasicMapper;
import cn.swenty.backstage.framework.database.plugin.PageView;
import cn.swenty.backstage.framework.service.ServiceFactory;

/**
 * 实现这个类的实体类可以直接操作增删改查
 * 
 * @author sunyang
 *
 */
public class Entity extends FormMap{
	
	public final static String PAGE_VIEW = "$paging";
	

	private static final long serialVersionUID = 1L;
	
	public Integer save(){
		return ServiceFactory.getMapper(BasicMapper.class).$save(Entity.newThis(this));
	}
	
	public Integer update(){
		return ServiceFactory.getMapper(BasicMapper.class).$update(Entity.newThis(this));
	}
	
	public Integer modify(){
		 Entity it = (Entity)this.clone();
		 if(it.load() == null){
			 return Entity.newThis(this).save();
		 }else{
			 return Entity.newThis(this).update();
		 }
	}
	
	public Integer updateFull(){
		return ServiceFactory.getMapper(BasicMapper.class).$updateFull(Entity.newThis(this));
	}
	
	public <T extends Entity> List<T> queryPage(){
		if(this.get(PAGE_VIEW)==null){
			this.put(PAGE_VIEW, new PageView(10, 1));
		}
		return  entity2List(ServiceFactory.getMapper(BasicMapper.class).$query(Entity.newThis(this)));
	}
	
	public <T extends Entity> List<T> queryPage(int pagesize){
		if(this.get(PAGE_VIEW)==null){
			this.put(PAGE_VIEW, new PageView(pagesize, 1));
		}
		return entity2List(ServiceFactory.getMapper(BasicMapper.class).$query(Entity.newThis(this)));
	}
	
	public <T extends Entity> List<T> query(){
		return  entity2List( ServiceFactory.getMapper(BasicMapper.class).$query(Entity.newThis(this)));
	}
	
	public <T extends Entity> T queryOne(){
		List<Entity> entities = ServiceFactory.getMapper(BasicMapper.class)
																	.$query(Entity.newThis(this));
		if(entities.size()>1){
			throw new RuntimeException(" query "+entities.size()+" data ,not one ");
		}else if(entities ==null || entities.size()==0){
			return null;
		}else{
			return  (T) entity2Object(entities.get(0));
		}
	}
	
	public <T extends Entity> T load(){
		TableSeg seg = this.getClass().getAnnotation(TableSeg.class);
		if(seg == null){
			throw new RuntimeException("con't get TableSeg,please check code");
		}
		Entity e = Entity.newThis(this);
		Object id = this.get(seg.id());
		e.clear();
		e.put(seg.id(), id);
		List<Entity> entities = ServiceFactory.getMapper(BasicMapper.class).$query(e);
		if(entities.size()>=1){
			return  (T) entity2Object(entities.get(0));
		}else{
			return null;
		}
	}
	protected <T extends Entity> List<T> entity2List(List<Entity> ens){
		List<Entity> entities = new ArrayList<>();
		try {
			
			for(Entity en:ens){
				Entity e = this.getClass().newInstance();
				e.putAll(en);
				entities.add(e);
			}
			
		} catch (InstantiationException | IllegalAccessException e1) {
			e1.printStackTrace();
		}
		return (List<T>) entities;
	}
	
	
	protected Entity entity2Object(Entity entity){
		Entity e = null;
		try {
			e = this.getClass().newInstance();
			e.putAll(entity);
		} catch (InstantiationException | IllegalAccessException e1) {
			e1.printStackTrace();
		}
		return e;
	}

	public int delete(){
		return ServiceFactory.getMapper(BasicMapper.class).$delete(Entity.newThis(this));
	}
	
	public static Entity newThis(Entity e){
		//重新加载一个新的数据
		Entity entity = null;
		try {
			entity = e.getClass().newInstance();
			entity.putAll(e);
		} catch (InstantiationException | IllegalAccessException e1) {
			e1.printStackTrace();
		}
		return entity;
	}
	
	public <T extends Entity> T add(String key,Object value){
		this.put(key.toLowerCase(), value);
		return (T) this;
	}

	
}
